<!DOCTYPE html>
<html>
<head>
    <title>文档管理</title>
    <link rel="stylesheet" href="__PLUGIN_TMPL__/public/layui/css/layui.css">
    <link rel="stylesheet" href="__STATIC__/js/jquery-ui/jquery-ui.css">
    <link rel="stylesheet" type="text/css" href="__STATIC__/js/ueditor/themes/iframe.css">
	<style scoped>
		pre{margin:.5em 0;padding:.4em .6em;border-radius:8px;background:#f8f8f8;}
		.pagebreak{display:block;clear:both !important;cursor:default !important;width: 100% !important;margin:0;}
		table.sortEnabled tr.firstRow th,table.sortEnabled tr.firstRow td{padding-right:20px;background-repeat: no-repeat;background-position: center right;}
		.selectTdClass{background-color:#edf5fa !important}
		table.noBorderTable td,table.noBorderTable th,table.noBorderTable caption{border:1px dashed #ddd !important}
		table{margin-bottom:10px;border-collapse:collapse;display:table;}
		td,th{padding: 5px 10px;border: 1px solid #DDD;}
		caption{border:1px dashed #DDD;border-bottom:0;padding:3px;text-align:center;}
		th{border-top:1px solid #BBB;background-color:#F7F7F7;}
		table tr.firstRow th{border-top-width:2px;}
		.ue-table-interlace-color-single{ background-color: #fcfcfc; }
		.ue-table-interlace-color-double{ background-color: #f7faff; }
		td p{margin:0;padding:0;}
		ol,ul{margin:0;pading:0;width:95%}li{clear:both;}
		ol.custom_cn{list-style:none;}ol.custom_cn li{background-position:0 3px;background-repeat:no-repeat}
		li.list-cn-paddingleft-1{padding-left:25px}
		li.list-cn-paddingleft-2{padding-left:40px}
		li.list-cn-paddingleft-3{padding-left:55px}
		ol.custom_cn1{list-style:none;}ol.custom_cn1 li{background-position:0 3px;background-repeat:no-repeat}
		li.list-cn1-paddingleft-1{padding-left:30px}
		li.list-cn1-paddingleft-2{padding-left:40px}
		li.list-cn1-paddingleft-3{padding-left:55px}
		ol.custom_cn2{list-style:none;}ol.custom_cn2 li{background-position:0 3px;background-repeat:no-repeat}
		li.list-cn2-paddingleft-1{padding-left:40px}
		li.list-cn2-paddingleft-2{padding-left:55px}
		li.list-cn2-paddingleft-3{padding-left:68px}
		ol.custom_num{list-style:none;}ol.custom_num li{background-position:0 3px;background-repeat:no-repeat}
		li.list-num-paddingleft-1{padding-left:25px}
		ol.custom_num1{list-style:none;}ol.custom_num1 li{background-position:0 3px;background-repeat:no-repeat}
		li.list-num1-paddingleft-1{padding-left:25px}
		ol.custom_num2{list-style:none;}ol.custom_num2 li{background-position:0 3px;background-repeat:no-repeat}
		li.list-num2-paddingleft-1{padding-left:35px}
		li.list-num2-paddingleft-2{padding-left:40px}
		li.list-dash{background-image:url(http://bs.baidu.com/listicon/dash.gif)}
		ul.custom_dash{list-style:none;}ul.custom_dash li{background-position:0 3px;background-repeat:no-repeat}
		li.list-dash-paddingleft{padding-left:35px}
		li.list-dot{background-image:url(http://bs.baidu.com/listicon/dot.gif)}
		ul.custom_dot{list-style:none;}ul.custom_dot li{background-position:0 3px;background-repeat:no-repeat}
		li.list-dot-paddingleft{padding-left:20px}
		.list-paddingleft-1{padding-left:0}
		.list-paddingleft-2{padding-left:30px}
		.list-paddingleft-3{padding-left:60px}
		.pagebreak{display:block;clear:both !important;cursor:default !important;width: 100% !important;margin:0;}

		/*.layui-legend-size{font-size:28px !important;}
		.layui-nopadding{padding:0 !important;}
		.layui-overflow{overflow:auto;}
		.layui-nomargin{margin: 0 !important;}
		.layui-bottom{border-bottom:1px solid rgb(226, 226, 226);}
		.layui-input{height:30px !important; }
		.layui-form-search-item .layui-inline{margin-bottom: 5px;} 
		.layui-form-search-item label{margin-left: 15px;}
		.layui-table-page {padding: 7px 0px 0px;}
		.layui-field-padding{border-width: 1px;padding:0 0 5px 10px;}
		.layui-form-select dl{max-height:600px;}*/
	</style>
</head>
<body>

<div class="layui-tab " lay-filter="demo-tab">
	<div class="layui-card">
		<div class="layui-card-header layui-bg-gray layui-bottom">
			<form class="layui-form" action="">
			      <div class="layui-input-inline" style="width:50%">
			        <select name="modules" lay-filter="list" lay-verify="required" lay-search="">
			          <option value="0">直接选择或搜索选择</option>
			          {$select|default=''}
			        </select>
			      </div>
			      <a href="javascript:;" class="layui-btn layui-btn-xs add-word" ><i class="layui-icon">&#xe654;</i>添加文件</a>
		          <a href="javascript:;" class="layui-btn layui-btn-xs add-file" ><i class="layui-icon">&#xe654;</i>添加目录</a>
			      <!-- <button type="submit" class="layui-btn layui-btn-sm layui-btn-primary " style="font-weight:800;" lay-submit="" lay-filter="demo2"><i class="layui-icon "></i>搜索</button> -->
			</form>
		</div>
	</div>
</div>	
	<!-- 选择内容显示 -->
	<div class="layui-container"  id="cont" hidden="true">
		<fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
		  <legend id="title" style="font-size: 30px;">此处标题</legend>
		</fieldset>		
		<blockquote class="layui-elem-quote layui-quote-nm" id="owner">此处文章信息</blockquote>
		<div class="layui-row">
			<div class="layui-col-md2"></div>
			<div class="layui-col-md12" class="layui-input-inline">
				<fieldset class="layui-elem-field" class="layui-input-inline">
				  	<legend id="btn">
					  	<div id="pa" class="layui-input-inline"></div>
					  	<div id="ed" class="layui-input-inline"></div>
					  	<div id="de" class="layui-input-inline"></div>
				  	</legend>
				  	<div class="layui-field-box" style="word-break:break-all; width:1150px;">
					  	<ul id="content" style="font-size: 16px;">
					  		
					  	</ul>
				  	</div>
				</fieldset>	
			</div>
			<div class="layui-col-md2"></div>
		</div>
	</div>

	<!-- 选择内容显示 -->
<include file='public/scripts'>
<script src="__STATIC__/js/jquery-ui/jquery-ui.js"></script>
<script type="text/javascript" src="__STATIC__/js/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="__STATIC__/js/ueditor/ueditor.all.min.js"></script>

<script type="text/javascript">

	  var form = layui.form;

	  //排序
	  function sort(){
	    $( "#content" ).sortable({
	        opactiy:0.6,
	        revert:true,
	        cursor:"move",
	        update:function(){
	            var new_sort = [];
	            $("#content").find('li').each(function(){
	                new_sort.push($(this).val());
	            });
	            $.ajax({
	                url: "{:cmf_plugin_url('Word://admin_index/sort')}",
	                type: "post",
	                data:{
	                    sort:new_sort,
	                }
	            });
	        }
	    });
	   }

	    //选择触发
		form.on('select(list)', function(data){
            // let selectText=data.elem[data.elem.selectedIndex].text;//得到被选中的文本值
            //去空格
            // selectText=selectText.replace(/\s+/g, "");
            // data.elem[data.elem.selectedIndex].text=selectText;//覆盖原文本值
            //选中的id进行跳转
			tz(data.elem[data.elem.selectedIndex].value);    
        });

		//触发文档内容获取
		function choose(id){

			var content = {};
			content['id'] = id;
			if(content['id']){
            	//选中的默认值
				$.ajax({
					url:"{:cmf_plugin_url('Word://admin_index/wordContent')}",
					data:content,
					//得到选中的内容
					success:function(data){
						if(data.type=='success'){
							// console.log(data.info);

							if(id != '0'){
								//标题显示
								$('#title').html(data.info.name);
								//文件拥有人显示
								$('#owner').html('本文件由<b>'+data.info.uname+'</b>创建');
							}else{
								$('#title').html('<h2>首页</h2>');
								$('#owner').hide();
							}
							
							//内容显示
							$('#content').html(data.info.content);
							//判断有无编辑权限
							if(data.info.edit == 1){
								$('#ed').html("<a href='javascript:;' class='layui-btn layui-btn-xs word-edit' onclick='edit("+data.info.id+","+data.info.type+")'><i class='layui-icon layui-icon-edit'></i>修改</a>");
							}else{
								$('#ed').html("");
							}
							//判断有无删除权限
							if(data.info.del == 1){
								$('#de').html("<a class='layui-btn layui-btn-xs word-edit' onclick='del("+data.info.id+")'><i class='layui-icon layui-icon-delete'></i>删除</a>");
							}else{
								$('#de').html("");
							}

							if(data.info.edit == 1 || data.info.parent_id){
								$('#pa').html("<a class='layui-btn layui-btn-xs word-edit' onclick='tz("+data.info.parent_id+")'><i class='layui-icon layui-icon-return'></i>返回</a>");
								$('#btn').show();
							}else{
								$('#btn').hide();
							}

							//显示整个组件
							$('#cont').removeAttr('hidden');
						}else if(data.type=='error'){
							layer.msg(data.msg);
						}
						// console.log(data);
					},
					error:function(data){
						layer.msg('查询失败');
						return false;
					}
				})	
			} 			 			
		}

		//修改界面
		function edit(id,type){
			let url = "";
			let add_url = "";
			let width = "";
			let height = "";
			let title = "";
			if(type == 1){
				url = "{:cmf_plugin_url('Word://admin_index/editFilePage')}?id="+id;
				add_url = "{:cmf_plugin_url('Word://admin_index/editFile')}";
				width = "800px";
				height = "600px";
				title = "修改目录";
			}else if(type == 2){
				url = "{:cmf_plugin_url('Word://admin_index/editWordPage')}?id="+id;
				add_url = "{:cmf_plugin_url('Word://admin_index/editWord')}";				
				width = "100%";
				height = "100%";
				title = "修改文档";
			}

			layer.open({
				type: 2//基本层类型  0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
				,title: title//层的标题
				,shadeClose: true//是否点击遮罩关闭
				,shade: 0.8//遮罩
				,area: [width, height]//宽高
				,btn:['保存当前编辑信息[ctrl+s..]','提交并关闭','关闭']//弹框按钮
				,fixed:false//固定
				,content: url //iframe的url
				,yes:function(index, layero){
					//调用子页面方法
					parentInfo = window["layui-layer-iframe" + index].callbackdata(index);
					if(parentInfo == false){
						return false;
					}
					// console.log(parentInfo);
					$.ajax({
						url:add_url,
						type:"post",
						data:parentInfo,
						success:function(data){
							if(data.type=='success'){
								layer.msg( '信息保存成功' );
							}else if(data.type=='error'){
								layer.msg(data.msg);
							}
							// console.log(data);
						},
						error:function(data){
							layer.msg('添加失败');
							return false;
						}
					})	
				}
				,btn2:function(index, layero){
					parentInfo = window["layui-layer-iframe" + index].callbackdata(index);
					if(parentInfo == false){
						return false;
					}
					$.ajax({
						url:add_url,
						type:"post",
						data:parentInfo,
						success:function(data){
							if(data.type=='success'){
								layer.msg(data.msg,{time:1000},function(){
									layer.closeAll();
								});
							}else if(data.type=='error'){
							    
								layer.msg( data.msg );
							} 
							// console.log(data);
						},
						error:function(data){
							layer.msg('添加失败');
							return false;
						}
					})	
				},btn3:function(){
				},end:function(){
					window.location.reload();
				}
			});			
		}

		//添加文件夹
		$('.add-file').on('click',function(){
			let url = "{:cmf_plugin_url('Word://admin_index/addFilePage')}";
			let add_url = "{:cmf_plugin_url('Word://admin_index/addFile')}";
			layer.open({
				type: 2//基本层类型  0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
				,title: '添加目录'//层的标题
				,shadeClose: true//是否点击遮罩关闭
				,shade: 0.8//遮罩
				,area: ['800px', '600px']//宽高
				,btn:['提交','关闭']//弹框按钮
				,fixed:false//固定
				,content: url //iframe的url
				,yes:function(index, layero){
					//调用子页面方法
					parentInfo = window["layui-layer-iframe" + index].callbackdata(index);
					if(parentInfo == false){
						return false;
					}
					console.log(parentInfo);
					$.ajax({
						url:add_url,
						type:"post",
						data:parentInfo,
						success:function(data){
							if(data.type=='success'){
								layer.msg(data.msg,{time:2000},function(){
									window.location.href = "{:cmf_plugin_url('Word://admin_index/index')}?id="+data.id;
									// choose(data.id);
									// window.location.reload();
									layer.closeAll();
								});
							}else if(data.type=='error'){
								layer.msg(data.msg);
							}
						},
						error:function(data){
							layer.msg('添加失败');
							return false;
						}
					})	
				}
				,btn2:function(index, layero){
				}
			});
		});

		//添加文档
		$('.add-word').on('click',function(){
			let url = "{:cmf_plugin_url('Word://admin_index/addWordPage')}";
			let add_url = "{:cmf_plugin_url('Word://admin_index/addWord')}";
			layer.open({
				type: 2//基本层类型  0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层）
				,title: '添加文档'//层的标题
				,shadeClose: true//是否点击遮罩关闭
				,shade: 0.8//遮罩
				,area: ['100%', '100%']//宽高
				,btn:['提交','关闭']//弹框按钮
				,fixed:false//固定
				,content: url //iframe的url
				,yes:function(index, layero){
					//调用子页面方法
					parentInfo = window["layui-layer-iframe" + index].callbackdata(index);
					if(parentInfo == false){
						return false;
					}					
					$.ajax({
						url:add_url,
						type:"post",
						data:parentInfo,
						success:function(data){
							if(data.type=='success'){
								layer.msg(data.msg,{time:2000},function(){
									window.location.href = "{:cmf_plugin_url('Word://admin_index/index')}?id="+data.id;
									// choose(data.id);
									window.location.reload();
									layer.closeAll();
								});
							}else if(data.type=='error'){
								layer.msg(data.msg);
							}
						},
						error:function(data){
							layer.msg('添加失败');
							return false;
						}
					})	
				}
				,btn2:function(index, layero){
				}
			});
		});

		//删除 
		function del(id){
			layer.confirm('确认删除?', {icon: 3, title:'提示'}, function(index){
				let url = "{:cmf_plugin_url('Word://admin_index/del')}";
				$.ajax({
		            url:url,
					type:'post',
		            data:{id:id},
		            success:function(data){
						let icon = ( data.code == 200 )  ? 1 : 5 ; 
							layer.msg( data.msg , { time:1200 , icon:icon },function(){
								tz(data.parent);
							});
		            },
		            error:function(data){
		            	console.log(data);
		                layer.msg('修改失败');
		                return false;
		            }
	        	})
			  
			  layer.close(index);
			});			
		}



		function tz(id){
			window.location.href = "{:cmf_plugin_url('Word://admin_index/index')}?id="+id;
		}

		<php>
				echo "choose('".$id."')";
		</php>

</script>
</body>
</html>